Page({
  /**
   * 页面的初始数据
   */
  data: {
    token: '',
    userInfo: {},
    code: '',
    isfirstAdmin: false,
    hasNew: false,
    // sec_admin 二级
    // cbrc 银监
    // fir_admin 一级
    // manager 客户经理
    // '' 所有
    // list: [{
    //     title: '账号信息',
    //     url: '/pages/personalInfo/personalInfo?type=3',
    //     roleList: ['fir_admin', 'manager', 'cbrc', 'sec_admin', ''],
    //     iconUrl: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/icon1.png"
    //   },
    //   // {
    //   //   title: '关注企业',
    //   //   url: '/sub_guanzhu/attention/attention',
    //   //   roleList: ['fir_admin', 'manager', 'cbrc', 'sec_admin', ''],
    //   //   iconUrl:"https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/icon1.png"
    //   // },
    //   {
    //     title: '需求对接',
    //     url: '/sub_xuqiu/page0/page0',
    //     roleList: ['fir_admin', 'manager', 'cbrc', 'sec_admin'],
    //     iconUrl: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/icon7.png"
    //   },
    //   // {
    //   //   title: '用户管理',
    //   //   url: '/packageB/pages/auditList/auditList',
    //   //   roleList: ['fir_admin', 'sec_admin']
    //   // },
    //   // {
    //   //   title: '审批记录',
    //   //   url: '/sub_shenpi/shenpijilu/shenpijilu',
    //   //   roleList: ['fir_admin', 'manager', 'sec_admin'],
    //   //   iconUrl: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/icon2.png"
    //   // },
    //   // {
    //   //   title: '分配个体工商户',
    //   //   url: '/pages/distribution/distribution',
    //   //   roleList: ['fir_admin', 'manager', 'cbrc', 'sec_admin']
    //   // },
    //   {
    //     title: '机构管理',
    //     url: '/sub_jigouguanli/jigoulist/jigoulist',
    //     roleList: ['fir_admin', 'cbrc', 'sec_admin'],
    //     iconUrl: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/icon3.png"
    //   },
    //   {
    //     title: '人员管理',
    //     url: '/sub_renyuanguanli/select_firstbank/select_firstbank',
    //     roleList: ['fir_admin', 'sec_admin', 'cbrc'],
    //     iconUrl: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/icon4.png"
    //   },
    //   {
    //     title: '分配企业',
    //     url: '/sub_allotPro/page_enter/page_enter',
    //     roleList: ['fir_admin', 'cbrc', 'sec_admin'],
    //     iconUrl: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/icon5.png"
    //   },
    //   {
    //     title: '企业认领',
    //     url: '/sub_company_claim/page1/page1',
    //     roleList: ['fir_admin', 'sec_admin', 'manager'],
    //     iconUrl: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/renling.png"
    //   },
    //   {
    //     title: '关于我们',
    //     url: '/pages/cxgcHome/cxgcHome',
    //     roleList: ['fir_admin', 'manager', 'cbrc', 'sec_admin', ''],
    //     iconUrl: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/icon6.png"
    //   },
    // ],
    showList: [],
    moreInfo: '',
    vipCode: '',
    isLogin: '',
    spNum: '',
    visitNum: '',
    list: [{
        value: 0,
        title: '账号信息',
        icon: 'https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/zhxx.png',
        roleList: ['fir_admin', 'manager', 'cbrc', 'sec_admin', ''],
        url: '/pages/personalInfo/personalInfo?type=3',
      },
      {
        value: 1,
        title: '审批记录',
        icon: 'https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/spjl.png',
        roleList: ['fir_admin', 'manager', 'sec_admin'],
        url: '/sub_shenpi/shenpijilu/shenpijilu',
      },
      {
        value: 2,
        title: '机构管理',
        icon: 'https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/jggl.png',
        roleList: ['fir_admin', 'cbrc', 'sec_admin'],
        url: '/sub_jigouguanli/jigoulist/jigoulist',
      },
      {
        value: 3,
        title: '人员管理',
        icon: 'https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/rygl.png',
        roleList: ['fir_admin', 'sec_admin', 'cbrc'],
        url: '/sub_renyuanguanli/select_firstbank/select_firstbank',
      },
      {
        value: 4,
        title: '分配企业',
        icon: 'https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/fpqy.png',
        roleList: ['fir_admin', 'cbrc', 'sec_admin'],
        url: '/sub_allotPro/page_enter/page_enter',
      },
      {
        value: 5,
        title: '企业认领',
        icon: 'https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/qyrl.png',
        roleList: ['fir_admin', 'sec_admin', 'manager'],
        url: '/sub_company_claim/page1/page1',
      },
      {
        value: 6,
        title: '需求对接',
        icon: 'https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/xqdj.png',
        roleList: ['fir_admin', 'manager', 'cbrc', 'sec_admin'],
        url: '/sub_xuqiu/page0/page0',
      },
      {
        value: 7,
        title: '关于我们',
        icon: 'https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/gywm.png',
        roleList: ['fir_admin', 'manager', 'cbrc', 'sec_admin', ''],
        url: '/pages/cxgcHome/cxgcHome',
      },
      // {
      //   value:8,
      //   title:'联系我们',
      //   icon:'https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/lxwm.png',
      //   roleList: ['fir_admin', 'manager', 'cbrc', 'sec_admin', ''],
      //   url:''
      // },
    ]
  },

  // 判断拥有的权限
  async allocation() {
    // const res = await getApp().globalData.request("/login/getCurrentUserInfo", { needToken: true }, "POST");
    // wx.setStorageSync('userInfo', res.data)
    // console.log(res, '最新的用户信息')
    const corpRole = wx.getStorageSync('userInfo').corpRole || ''
    if (!corpRole) {
      this.setData({
        showList: [
          // {
          //   title: '关于我们',
          //   url: '/pages/cxgcHome/cxgcHome',
          //   roleList: ['fir_admin', 'manager', 'cbrc', 'sec_admin', ''],
          //   iconUrl: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/icon6.png"
          // },
          // {
          //   title: '审批记录',
          //   url: '/sub_shenpi/shenpijilu/shenpijilu',
          //   roleList: ['fir_admin', 'manager', 'cbrc', 'sec_admin', ''],
          //   iconUrl: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/icon2.png"
          // },
          // {
          //   title: '关注企业',
          //   url: '/sub_monitor/attention/attention',
          //   roleList: ['fir_admin', 'manager', 'cbrc', 'sec_admin', ''],
          //   iconUrl:"https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/icon1.png"
          // },
          // {
          //   title: '需求对接',
          //   url: '/sub_xuqiu/page0/page0',
          //   roleList: ['fir_admin', 'manager', 'cbrc', 'sec_admin', ''],
          //   iconUrl:"https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/icon7.png"
          // },
        ]
      })
      return
    }
    if (corpRole.length === 0) {
      corpRole.push('')
    }
    const list = this.data.list.filter(item => {
      let num1 = corpRole.length + item.roleList.length
      let num2 = [...new Set([...corpRole, ...item.roleList])].length
      if (num1 === num2) {
        return false
      } else {
        return true
      }
    })
    this.setData({
      showList: list
    })
    console.log(this.data.showList, 'showList')
  },

  toInfo() {
    wx.navigateTo({
      url: '/pages/news/news',
    })
  },
  goNews() {
    wx.navigateTo({
      url: '/sub_shenpi/shenpijilu/shenpijilu'
    })
    // // const qxList = ['fir_admin', 'manager', 'sec_admin']
    // if (this.data.userInfo.corpRole.length > 0) {
    //   if (this.data.userInfo.corpRole.includes('fir_admin') ||
    //     this.data.userInfo.corpRole.includes('manager') || this.data.userInfo.corpRole.includes('sec_admin')) {
    //     wx.navigateTo({
    //       url: '/sub_shenpi/shenpijilu/shenpijilu'
    //     })
    //   }
    // } else {

    // }
  },
  goGz() {
    wx.navigateTo({
      url: '/sub_guanzhu/concern/concern',
    })
  },
  goVisit() {
    // wx.navigateTo({
    //   url: '/sub_visit_center/visitList/visitList',
    // })
    wx.navigateTo({
      url: "/pages/visitList/visitList",
    });
  },
  goAttention() {
    wx.navigateTo({
      url: '/sub_monitor/attention/attention'
    })
  },
  dealBind(e) {
    const type = e.currentTarget.dataset.type
    if (type === '2') {
      wx.navigateTo({
        url: '/pages/accountBind/accountBind?type=1'
      })
    } else {
      wx.navigateTo({
        url: '/pages/personalInfo/personalInfo?type=2'
      })
    }
  },

  // 获取机构信息
  getMoreInfo() {
    let prams = {
      userId: this.data.userInfo.userId
    }
    // console.log(prams);
    getApp().globalData.request('/corpVisit/query_user_info', prams, "POST", ).then(res => {
      console.log(res, 'moreInfo');
      // res.data.roleName = res.data.roles.length > 0 ? res.data.roles.map(val => val.roleName).join(',') : ''
      if (res.data) {
        this.setData({
          moreInfo: res.data
        })
      } else {
        this.setData({
          moreInfo: {}
        })
      }
    })
  },
  goFirm() {
    wx.navigateTo({
      url: "/pages/accountAuth/auth_management/auth_management",
    });
  },
  getCode() {
    wx.login({
      success: res => {
        this.setData({
          code: res.code
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},
  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    this.updateUserinfo(avatarUrl, '')
  },
  getUserinfo() {
    const _this = this
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        const {
          avatarUrl,
          nickName
        } = res.userInfo
        // 保存用户信息
        _this.updateUserinfo(avatarUrl, nickName)
      }
    })
  },
  async updateUserinfo(avatarUrl, nickName) {
    const params = {
      userId: this.data.userInfo.userId,
      userName: nickName,
      avatarUrl: avatarUrl,
    }
    console.log(params, 'params')
    const res = await getApp().globalData.request(
      "/api/userApi/updateUserinfo", params,
      "POST"
    );
    // 重新获取用户信息
    console.log(res, 'res111')
    this.initInfos()
  },
  async initInfos() {
    const {
      data
    } = await getApp().globalData.request("/login/getCurrentUserInfo", {}, "POST");
    console.log(data, 'data')
    // data.avatarUrl = data.avatarUrl || 'https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/2.png'
    wx.setStorageSync('userInfo', data)
    this.setData({
      userInfo: data,
    });
    this.getMoreInfo()
  },
  goUser() {
    wx.setStorageSync("token", "cxgc123456");
    wx.navigateTo({
      url: "/pages/launch_start/launch_start?type=1",
    });
  },

  goItemPage(e) {
    console.log(e.currentTarget.dataset)
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    });
  },

  async getNews() {
    const res = await getApp().globalData.request("/user/query_has_update", {}, "POST");
    // console.log(res, 'res1111')
    if (res.data > 0) {
      this.setData({
        hasNew: true
      })
    } else {
      this.setData({
        hasNew: false
      })
    }
  },

  goDetail() {
    if (this.data.token) {
      // const userInfo = JSON.stringify(this.data.userInfo)
      wx.navigateTo({
        url: '/pages/personalInfo/personalInfo?type=1'
      })
    }
  },
  async initPage() {
    // 获取当前人的账号级别
    const vipCode = await getApp().globalData.checkVipStatus('N')
    this.setData({
      userInfo: wx.getStorageSync('userInfo') || {},
      vipCode: vipCode,
    })
    this.setData({
      isLogin: this.data.userInfo.userId ? 'Y' : 'N'
    })
    console.log(this.data.vipCode, 'vipCode');
    if (this.data.userInfo.userId) {
      // 查询是否有新增
      this.getNews()
      await this.initInfos()
    } else {
      wx.setNavigationBarTitle({
        title: '登录', //页面标题为路由参数
      });
      const {
        data
      } = await getApp().globalData.request("/login/getCurrentUserInfo", {}, "POST");
      console.log(data, 'data')
      // data.avatarUrl = data.avatarUrl || 'https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/2.png'
      wx.setStorageSync('userInfo', data)
      this.setData({
        userInfo: data,
      });
    }
    this.allocation()
  },
  async getSpNum() {
    const res = await getApp().globalData.request(
      "/user/apply_wait_count", {},
      "POST"
    )
    this.setData({
      spNum: res.data || 0
    })
  },

  async getVisit() {
    const params = {
      batchNum: "",
      pageNum: 1,
      pageSize: 25,
      questionStatus: "",
      status: "未走访",
      type: 0,
      userId: "e174a7443fa6484f88ee69b453bef591",
      userLat: 31.268449258592327,
      userLng: 120.63862787231987,
    }
    const res = await getApp().globalData.request(
      "/visit/getList", params,
      "POST"
    )
    this.setData({
      visitNum: res.data.total || 0
    })
  },

  async loginOut() {
    wx.removeStorageSync("token");
    wx.removeStorageSync("userInfo");
    wx.switchTab({
      url: "/pages/shouye/shouye",
    });
    // const res = await getApp().globalData.request("/login/logout", {}, "POST");
    // if (res) {
    //   wx.removeStorageSync("token");
    //   wx.removeStorageSync("userInfo");
    //   wx.switchTab({
    //     url: "/pages/shouye/shouye",
    //   });
    // } else {
    //   wx.removeStorageSync("token");
    //   wx.removeStorageSync("userInfo");
    //   wx.switchTab({
    //     url: "/pages/shouye/shouye",
    //   });
    // }
  },
  openSq() {
    wx.requestSubscribeMessage({
      tmplIds: [
        '7CkKqv0VgrM7EDek6GwIuLpqtRUgWKtOvwEYJm5XRQo', 'zxobyVTUXFvndLgPZnK7gwsjG473wmeIKPjFIw4G2c4',
        // 新2
        'wVMAvoZCMqXQOItyW2jDxUumGUC9gZR_23bNut227qU',
        // 新1
        // '6dq-ZVqqp7BEVw0K9J3Vz4frmpwBlCOMy1qrTpRk-Ik'
        // 旧
        // 'ZSoj0iK_emlxEP78ycYpwuY8AL8vEnZyIjmLq1v_5UY'
      ],
      success(res) {
        console.log('我是订阅');
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initPage()
    this.getSpNum()
    try {
      this.getVisit()
    } catch (error) {
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {},
});



// // key1:  神经内科， key2: 呼吸内科
// {
//   key1: [
//     {
//       name: '李浩然',
//       age: 24,
//       sex: '男'
//     },
//     {
//       name: '李浩然1',
//       age: 24,
//       sex: '男'
//     },
//   ],
//   key2: [
//     {
//       name: '李浩然',
//       age: 24,
//       sex: '男'
//     },
//     {
//       name: '李浩然1',
//       age: 24,
//       sex: '男'
//     },
//   ]
// }